package com.chenjl.api;

import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.kafka.common.Cluster;
import org.apache.kafka.common.Node;
import org.apache.kafka.common.PartitionInfo;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * 获取集群信息维护
 * 启动时默认一个 Cluster cluster = Cluster.bootstrap(addresses);
 * 后续根据Broker更新Cluster
 * 
 * 2016-9-18 15:26:21
 * @author chenjinlong
 */
public class TestOnCluster {
	private static final Logger logger = LoggerFactory.getLogger(TestOnCluster.class);
	
	@Test
	public void test() {
		Collection<Node> nodes = new ArrayList<Node>();
		Node node01 = new Node(101,"192.168.245.127",9092);
		Node node02 = new Node(102,"192.168.245.128",9092);
		Node node03 = new Node(103,"192.168.245.129",9092);
		nodes.add(node01);
		nodes.add(node02);
		nodes.add(node03);
		Node[] nodeArray = {node01,node02,node03};
		
		Collection<PartitionInfo> partitionInfos = new ArrayList<PartitionInfo>();
		partitionInfos.add(new PartitionInfo("topic01",1,node01,nodeArray,nodeArray));
		partitionInfos.add(new PartitionInfo("topic01",2,node01,nodeArray,nodeArray));
		partitionInfos.add(new PartitionInfo("topic01",0,node01,nodeArray,nodeArray));
		
		Set<String> unauthorizedTopics = new HashSet<String>();
		unauthorizedTopics.add("topic01");
		unauthorizedTopics.add("topic02");
		
		Cluster cluster = new Cluster(nodes,partitionInfos,unauthorizedTopics);
		
		logger.info("isBootstrapConfigured : {}",cluster.isBootstrapConfigured());
	}
	public static void main(String[] args) {
		List<InetSocketAddress> addresses = new ArrayList<InetSocketAddress>();
		addresses.add(new InetSocketAddress("192.168.245.127",9092));
		addresses.add(new InetSocketAddress("192.168.245.128",9092));
		addresses.add(new InetSocketAddress("192.168.245.129",9092));
		
		Cluster cluster = Cluster.bootstrap(addresses);
		
		logger.info("isBootstrapConfigured : {}",cluster.isBootstrapConfigured());
	}
}